﻿using System.Linq;
using System.Web.Mvc;
using SacNetUG.Filters;
using SacNetUG.Models;

namespace SacNetUG.Controllers
{
    [Authorize]
    public partial class UserController : Controller
    {
        public virtual ActionResult Index()
        {
            var db = new SacNetUGDataContext();

            //paging would be handled here
            return View(Views.Index, db.Users.OrderBy(u => u.FirstName));
        }

        public virtual ActionResult Create()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public virtual ActionResult Create(User user)
        {
            try
            {

                user.SanitizeInput();
                user.AuthID = "";
                if (user.EmailAddress == null)
                {
                    user.EmailAddress = "";
                }
                var repository = new SacNetUGRepository();
                repository.Db.InsertOnSubmit<User>(user);
                repository.Db.SubmitChanges();
                return RedirectToAction("Index");


                //if (!ModelState.IsValid)
                //    return View(user);

                //var db = new SacNetUGDataContext();
                //db.Users.InsertOnSubmit(user);
                //db.SubmitChanges();

                ////return RedirectToAction("Index", "User");
                //return RedirectToAction(MVC.User.Index());
            }
            catch
            {
                return View(user);
            }
        }

        public virtual ActionResult Delete(int id)
        {
            var repo = new SacNetUGRepository();
            return View("Delete", repo.GetUser(id));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public virtual ActionResult Delete(int id, string confirmButton)
        {
            var repo = new SacNetUGRepository();
            repo.Db.DeleteAllOnSubmit(repo.Db.Table<UserRole>().Where(u => u.UserId == id));
            repo.Db.DeleteOnSubmit(repo.GetUser(id));

            repo.Save();
            return RedirectToAction("Index");
        }



        public virtual ActionResult Edit(int id)
        {
            var repo = new SacNetUGRepository();

            return View(repo.GetUser(id));
        }

        //
        // POST: /Secure/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public virtual ActionResult Edit(int id, User user)
        {
            try
            {
                user.SanitizeInput();

                string[] props = new string[] {"FirstName","LastName","EmailAddress","ImageUrl", "Bio"};
                var db = new SacNetUGDataContext();
                var userFromDb = db.Users.FirstOrDefault(u => u.Id == id);
                UpdateModel(userFromDb, props);

                if (userFromDb.EmailAddress == null)
                {
                    userFromDb.EmailAddress = "";
                }

                db.SubmitChanges();

                return RedirectToAction(MVC.User.Index());
            }
            catch
            {
                return View(user);
            }
        }
    }
}